#include"GrasviationListener.h"
#include"Graviation.h"

GrasviationListener::GrasviationListener(Graviation * main)
{
	app = main;
}

void GrasviationListener::BeginContact(b2Contact * contact)
{
	b2Body* bodyA = contact->GetFixtureA()->GetBody();
	b2Body* bodyB = contact->GetFixtureB()->GetBody();
	std::vector<b2Body*> result = checkWithTarget(bodyA, bodyB, app->bird, app->sensor);
	if (result.size() > 0)
	{
		isBirdSensor = true;
	}
}

void GrasviationListener::EndContact(b2Contact * contact)
{
	b2Body* bodyA = contact->GetFixtureA()->GetBody();
	b2Body* bodyB = contact->GetFixtureB()->GetBody();
	std::vector<b2Body*> result = checkWithTarget(bodyA, bodyB, app->bird, app->sensor);
	if (result.size() > 0)
	{
		isBirdSensor = false;
	}
}

void GrasviationListener::PreSolve(b2Contact * contact, const b2Manifold * oldManifold)
{
}

void GrasviationListener::PostSolve(b2Contact * contact, const b2ContactImpulse * impulse)
{
}

std::vector<b2Body*> GrasviationListener::checkWithTarget(b2Body * checkA, b2Body * checkB, b2Body * targetA, b2Body * targetB)
{
	std::vector<b2Body*> checkResult;
	if (checkA == targetA && checkB == targetB)
	{
		checkResult.push_back(checkA);
		checkResult.push_back(checkB);
	}
	else if (checkA == targetB && checkB == targetA)
	{
		checkResult.push_back(checkB);
		checkResult.push_back(checkA);
	}
	return checkResult;
}
